using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Windows.Input;
using Microsoft.Data.SqlClient;

namespace RugbyClub.ViewModels
{
    public class PresidentWindowViewModel : INotifyPropertyChanged
    {
        private string _coachName;
        private string _playerName;
        private string _playerAge;
        private string _playerSkills;
        private string _matchInfo;

        public string CoachName
        {
            get => _coachName;
            set
            {
                _coachName = value;
                OnPropertyChanged();
            }
        }

        public string PlayerName
        {
            get => _playerName;
            set
            {
                _playerName = value;
                OnPropertyChanged();
            }
        }

        public string PlayerAge
        {
            get => _playerAge;
            set
            {
                _playerAge = value;
                OnPropertyChanged();
            }
        }

        public string PlayerSkills
        {
            get => _playerSkills;
            set
            {
                _playerSkills = value;
                OnPropertyChanged();
            }
        }

        public string MatchInfo
        {
            get => _matchInfo;
            set
            {
                _matchInfo = value;
                OnPropertyChanged();
            }
        }

        public ICommand AddCoachCommand { get; }
        public ICommand AddPlayerCommand { get; }

        public PresidentWindowViewModel()
        {
            AddCoachCommand = new RelayCommand(AddCoach);
            AddPlayerCommand = new RelayCommand(AddPlayer);
            LoadMatchInfo();
        }

        private void AddCoach()
        {
            // Implement your SQL Server logic to add a coach
            using (SqlConnection connection = new SqlConnection("your_connection_string"))
            {
                connection.Open();
                string query = "INSERT INTO Coaches (Name) VALUES (@Name)";
                using (SqlCommand command = new SqlCommand(query, connection))
                {
                    command.Parameters.AddWithValue("@Name", CoachName);
                    command.ExecuteNonQuery();
                }
            }
        }

        private void AddPlayer()
        {
            // Implement your SQL Server logic to add a player
            using (SqlConnection connection = new SqlConnection("your_connection_string"))
            {
                connection.Open();
                string query = "INSERT INTO Players (Name, Age, Skills) VALUES (@Name, @Age, @Skills)";
                using (SqlCommand command = new SqlCommand(query, connection))
                {
                    command.Parameters.AddWithValue("@Name", PlayerName);
                    command.Parameters.AddWithValue("@Age", PlayerAge);
                    command.Parameters.AddWithValue("@Skills", PlayerSkills);
                    command.ExecuteNonQuery();
                }
            }
        }

        private void LoadMatchInfo()
        {
            // Implement your SQL Server logic to load match information
            using (SqlConnection connection = new SqlConnection("your_connection_string"))
            {
                connection.Open();
                string query = "SELECT Date, Location FROM Matches";
                using (SqlCommand command = new SqlCommand(query, connection))
                {
                    using (SqlDataReader reader = command.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            MatchInfo += $"Date: {reader["Date"]}, Location: {reader["Location"]}\n";
                        }
                    }
                }
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

